﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Compiler
    Public NotInheritable Class ValidationManager
        Implements IServiceProvider
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider)
            Me.New(serviceProvider, True)
        End Sub

        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal validateChildActivities As Boolean)
            Me.hashOfValidators = New Hashtable
            Me.validateChildActivities = True
            Me.serviceProvider = serviceProvider
            Me.validateChildActivities = validateChildActivities
        End Sub

        Public Function GetService(ByVal serviceType As Type) As Object
            Return Me.serviceProvider.GetService(serviceType)
        End Function

        Public Function GetValidators(ByVal type As Type) As Validator()
            If Me.hashOfValidators.Contains(type) Then
                Return DirectCast(Me.hashOfValidators.Item(type), List([Of] Validator)).ToArray
            End If
            Dim list As New List([Of] Validator)
            Dim validator As Validator
            For Each validator In ComponentDispenser.CreateComponents(type, GetType(ActivityValidatorAttribute))
                list.Add(validator)
            Next
            Me.hashOfValidators.Item(type) = list
            Return list.ToArray
        End Function


        ' Properties
        Public ReadOnly Property Context As ContextStack
            Get
                If (Me.context Is Nothing) Then
                    Me.context = New ContextStack
                End If
                Return Me.context
            End Get
        End Property

        Public ReadOnly Property ValidateChildActivities As Boolean
            Get
                Return Me.validateChildActivities
            End Get
        End Property


        ' Fields
        Private context As ContextStack
        Private hashOfValidators As Hashtable
        Private serviceProvider As IServiceProvider
        Private validateChildActivities As Boolean
    End Class
End Namespace

